/**

 */
package com.adkfp.action;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adkfp.bean.ADKDeptBean;
import com.adkfp.bean.ADKDoorBean;
import com.adkfp.common.ControllerTime;
import com.adkfp.common.DoorNodes;
import com.adkfp.common.UserRight;
import com.adkfp.service.IADKDoorService;

import net.sf.json.JSONObject;

/**
 * class name:SelectclassAction <BR>
 * class description: please write your description <BR>
 * Remark: <BR>
 * @version 1.00 2019年1月23日
 * @author ADK)zhoulin
 */
@Controller
@RequestMapping("aDKDoor")
public class ADKDoorAction {
	@Resource(name="aDKDoorService")
  private IADKDoorService aDKDoorService;
	
	 /**
 	* Method name: selectAll <BR>
 	* Description: 查询所有记录 <BR>
 	* Remark: <BR>
 	* @param bean
 	* @return  Object<BR>
 	*/
	@RequestMapping("selectAll")
	@ResponseBody
	public Object selectAll(ADKDoorBean bean) throws ClassNotFoundException, SQLException, IOException{
		/*List<ADKDoorBean> list = new ArrayList<>();
		list = aDKDoorService.findByCondition(bean);
		long count = aDKDoorService.findByConditionCount(bean);
		return LayuiUtil.data(count, list);*/
		
		InputStream in =  this.getClass().getClassLoader().getResourceAsStream("/database.properties");
		Properties properties = new Properties();
		properties.load(in);
		
		//获取key对应的value值
		System.out.print("1111=================================");
		String dbdriver = properties.getProperty("jdbc.driverClass");//"com.mysql.jdbc.Driver");
		String dbhost =properties.getProperty("jdbc.jdbcUrl") ;//"jdbc:mysql://192.168.0.198:3306/qudao";
		String dbuser = properties.getProperty("jdbc.user","root");
		String dbpassword = properties.getProperty("jdbc.password", "123456");
		DoorNodes dt=new DoorNodes(dbdriver,dbhost,dbuser,dbpassword);
		return dt.getResult();
	}
	
	/**
	 *转发到页面 
	 */
	@RequestMapping("index")
	public String index() {
		return "aDKDoor/DoorTree";
	}
	
	/**
	 *转发到页面 
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
/*	@RequestMapping("Door")
	public String Door() {
		return "aDKDoor/Door";
	}*/
	
	 //门信息的增加（修改）
	 @RequestMapping("doorInandUp")
	 @ResponseBody
	 public void deptInandUp(HttpServletRequest request,ADKDoorBean doorBean) throws ClassNotFoundException, SQLException{
		 	 //常开
			 String startopen1=null;
			 String endopen1=null;
			 String startopen2=null;
			 String endopen2=null;
			 String startopen3=null;
			 String endopen3=null;
		 	 String opentime=request.getParameter("opentime");
		 	 System.err.println("opentime"+opentime);
		 	if(opentime==null) {
		 		opentime="AA";
		 		startopen1="0000";
		 		endopen1="0000";
		 		startopen2="0000";
		 		endopen2="0000";
		 		startopen3="0000";
		 		endopen3="0000";
		 	}else if(opentime.equals("55")) {
		 		//opentime="55";
				//常开第一时间段
				String[] so1=request.getParameter("startopen1").split(":");//前台值传回后台
				startopen1=so1[0]+so1[1];
			    String[] eo1=request.getParameter("endopen1").split(":");
				endopen1=eo1[0]+eo1[1];
				//常开第二时间段
				String[] so2=request.getParameter("startopen2").split(":");
				startopen2=so2[0]+so2[1];
				String[] eo2=request.getParameter("endopen2").split(":");
				endopen2=eo2[0]+eo2[1];
				//常开第三时间段
				String[] so3=request.getParameter("startopen3").split(":");
				startopen3=so3[0]+so3[1];
				String[] eo3=request.getParameter("endopen3").split(":");
				endopen3=eo3[0]+eo3[1];
				
		 	}
		 	 
			 
			 
			 //常闭
			 String startclose1=null;
			 String endclose1=null;
			 String startclose2=null;
			 String endclose2=null;
			 String startclose3=null;
			 String endclose3=null;
			 String closetime=request.getParameter("closetime");
			 if(closetime==null) {
				closetime="AA";
				startclose1="0000";
				endclose1="0000";
				startclose2="0000";
				endclose2="0000";
				startclose3="0000";
				endclose3="0000";
			 }else if(closetime.equals("55")) {
				 //closetime="55";
				 //常闭第一时间段
				 String[] sc1=request.getParameter("startclose1").split(":");
				 startclose1=sc1[0]+sc1[1];
				 String[] ec1=request.getParameter("endclose1").split(":");
				 endclose1=ec1[0]+ec1[1];
				 //常闭第二时间段
				 String[] sc2=request.getParameter("startclose2").split(":");
				 startclose2=sc2[0]+sc2[1];
				 String[] ec2=request.getParameter("endclose2").split(":");
				 endclose2=ec2[0]+ec2[1];
				 //常闭第三时间段
				 String[] sc3=request.getParameter("startclose3").split(":");
				 startclose3=sc3[0]+sc3[1];
				 String[] ec3=request.getParameter("endclose3").split(":");
				 endclose3=ec3[0]+ec3[1];
			 }
			 
			 
			 //报警设防
			 String stimep1=null;
			 String etimep1=null;
			 String stimep2=null;
			 String etimep2=null;
			 String stimep3=null;
			 String etimep3=null;
			 String otimep=request.getParameter("otimep");
			 if(otimep==null) {
				  otimep="AA";
				  stimep1="0000";
				  etimep1="0000";
				  stimep2="0000";
				  etimep2="0000";
				  stimep3="0000";
				  etimep3="0000";
			 }else if(otimep.equals("55")){
				//otimep="55";
				//报警设防第一时间段
				 String[] stp1=request.getParameter("stimep1").split(":");
				  stimep1=stp1[0]+stp1[1];
				 String[] etp1=request.getParameter("etimep1").split(":");
				  etimep1=etp1[0]+etp1[1];
				 //报警设防第二时间段
				 String[] stp2=request.getParameter("stimep2").split(":");
				  stimep2=stp2[0]+stp2[1];
				 String[] etp2=request.getParameter("etimep2").split(":");
				  etimep2=etp2[0]+etp2[1];
				 //报警设防第三时间段
				 String[] stp3=request.getParameter("stimep3").split(":");
				  stimep3=stp3[0]+stp3[1];
				 String[] etp3=request.getParameter("etimep3").split(":");
				  etimep3=etp3[0]+etp3[1];
			 }
			 
			 
			 JSONObject jsonObject = new JSONObject();
			 //常开
			 jsonObject.put("opentime",opentime);
			 //常开第一时间段
			 jsonObject.put("startopen1", startopen1);
			 jsonObject.put("endopen1", endopen1);
			 //常开第二时间段
		     jsonObject.put("startopen2", startopen2);
			 jsonObject.put("endopen2", endopen2);
			 //常开第三时间段
			 jsonObject.put("startopen3", startopen3);
			 jsonObject.put("endopen3", endopen3);
			 
			 //常闭
			 jsonObject.put("closetime",closetime);
			 //常闭第一时间段
			 jsonObject.put("startclose1", startclose1);
			 jsonObject.put("endclose1", endclose1);
			 //常闭第二时间段
			 jsonObject.put("startclose2", startclose2);
			 jsonObject.put("endclose2", endclose2);
			 //常闭第三时间段
			 jsonObject.put("startclose3", startclose3);
			 jsonObject.put("endclose3", endclose3);
			 
			 //报警设防
			 jsonObject.put("otimep",otimep);
			//报警设防第一时间段
			 jsonObject.put("stimep1", stimep1);
			 jsonObject.put("etimep1", etimep1);
			//报警设防第二时间段
			 jsonObject.put("stimep2", stimep2);
			 jsonObject.put("etimep2", etimep2);
			//报警设防第三时间
			 jsonObject.put("stimep3", stimep3);
			 jsonObject.put("etimep3", etimep3);
			 //常开休息日
			 JSONObject jsonObject1 = new JSONObject();
			 
			 jsonObject1.put("sun", request.getParameter("sun"));
			 jsonObject1.put("mon", request.getParameter("mon"));
			 jsonObject1.put("tues", request.getParameter("tues"));
			 jsonObject1.put("wed", request.getParameter("wed"));
			 jsonObject1.put("thur", request.getParameter("thur"));
			 jsonObject1.put("fri", request.getParameter("fri"));
			 jsonObject1.put("sat", request.getParameter("sat"));
			 System.out.println("前台传过来的星期日是：---"+request.getParameter("sun"));
			 System.out.println("前台传过来的星期六是：---"+request.getParameter("sat"));
			 System.out.println("前台传过来的星期五是：---"+request.getParameter("fri"));
			 System.out.println("前台传过来的星期四是：---"+request.getParameter("thur"));
			 String weektime=ControllerTime.DataTimeunpack(jsonObject1);
			 jsonObject.put("weektime", weektime);
			 
			 //常闭休息日
			 JSONObject jsonObject2 = new JSONObject();
			 
			 jsonObject2.put("unsun", request.getParameter("unsun"));
			 jsonObject2.put("unmon", request.getParameter("unmon"));
			 jsonObject2.put("untues", request.getParameter("untues"));
			 jsonObject2.put("unwed", request.getParameter("unwed"));
			 jsonObject2.put("unthur", request.getParameter("unthur"));
			 jsonObject2.put("unfri", request.getParameter("unfri"));
			 jsonObject2.put("unsat", request.getParameter("unsat"));
			 String weektimeB=ControllerTime.DataTimeunpackB(jsonObject2);
			 jsonObject.put("weektimeB", weektimeB);
			 System.out.println("---常闭前台传过来的星期日是：---"+request.getParameter("unsun"));
			 System.out.println("---常闭前台传过来的星期六是：---"+request.getParameter("unsat"));
			 System.out.println("---常闭前台传过来的星期五是：---"+request.getParameter("unfri"));
			 System.out.println("---常闭前台传过来的星期四是：---"+request.getParameter("unthur"));
			 
			 JSONObject jsonObject3 = new JSONObject();
			 
			 jsonObject3.put("sun1", request.getParameter("sun1"));
			 jsonObject3.put("mon1", request.getParameter("mon1"));
			 jsonObject3.put("tues1", request.getParameter("tues1"));
			 jsonObject3.put("wed1", request.getParameter("wed1"));
			 jsonObject3.put("thur1", request.getParameter("thur1"));
			 jsonObject3.put("fri1", request.getParameter("fri1"));
			 jsonObject3.put("sat1", request.getParameter("sat1"));
			 System.out.println("报警前台传过来的星期日是：---"+request.getParameter("sun1"));
			 System.out.println("报警前台传过来的星期六是：---"+request.getParameter("sat1"));
			 System.out.println("报警前台传过来的星期五是：---"+request.getParameter("fri1"));
			 System.out.println("报警前台传过来的星期四是：---"+request.getParameter("thur1"));
			 String wtimep=ControllerTime.DataTimeunpackPo(jsonObject3);
			 System.out.println("从方法ControllerTime传过来的wtimep是："+wtimep);
			 jsonObject.put("wtimep", wtimep);
			 
			 
			 String upd1=ControllerTime.longOpenpack(jsonObject);
			 doorBean.setNoncparam(upd1);
			 String upd2=ControllerTime.OpenPolicepack(jsonObject);
			 doorBean.setNowarntime(upd2);
			 if(doorBean.getAutoid()!=null && doorBean.getAutoid()!=0) {
				 //修改
				 aDKDoorService.update(doorBean);
			 }/*else {
				 //增加
				 aDKDoorService.insert(doorBean);
			 }*/
			 
			 InputStream in =  this.getClass().getClassLoader().getResourceAsStream("/database.properties");
				Properties properties = new Properties();
				try {
					properties.load(in);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//获取key对应的value值
				String dbdriver = properties.getProperty("jdbc.driverClass");//"com.mysql.jdbc.Driver");
				String dbhost =properties.getProperty("jdbc.jdbcUrl") ;//"jdbc:mysql://192.168.0.198:3306/qudao";
				String dbuser = properties.getProperty("jdbc.user","root");
				String dbpassword = properties.getProperty("jdbc.password", "123456");
				UserRight right = new UserRight(dbdriver,dbhost,dbuser,dbpassword);
				System.out.println("门参数的autoID是-------------------"+doorBean.getAutoid());
				//right.getDoorParamter(doorBean.getAutoid());
		 }	
		
/*	//门信息的删除
	@RequestMapping("deletedoor")
	@ResponseBody
	public String deletedoor(String id) {
		if(id!=null&&!id.equals("")) {
			aDKDoorService.delete(Integer.parseInt(id));
				return "删除成功！";
			}else {
				return "删除失败！";
			}
	}*/
	
	//转发页面到添加(修改)门信息页面 
	@RequestMapping("Door")
	public String Door(HttpServletRequest request, Integer id,HttpServletResponse response) {
		ADKDoorBean doorBean = new ADKDoorBean();	
		if(id != null && !id.equals(" ")){
			doorBean=aDKDoorService.findById(id);
		}
		request.setAttribute("doorBean",doorBean);
		//回填显示时间在页面（拆开）
		request.setAttribute("non",ControllerTime.longOpen(doorBean.getNoncparam()));
		request.setAttribute("now",ControllerTime.OpenPolice(doorBean.getNowarntime()));
		return "aDKDoor/Door";
	}
	
	//增加门信息
	/*@RequestMapping("insertdoor")
	@ResponseBody
	public void insertdoor(HttpServletRequest request,ADKDoorBean bean) {
		if(bean.getAutoid()!=null&&bean.getAutoid()!=0) {
			aDKDoorService.update(bean);
		}else {
			aDKDoorService.insert(bean);
		}
	}*/
	
	/*//用于修改页面(查找id)
			@RequestMapping("findId")
			public String findId(HttpServletRequest request,String autoid,ADKDoorBean bean) {
				if(autoid != null && !autoid.equals("")) {
					bean=aDKDoorService.findById(Integer.parseInt(autoid));
				}
				request.setAttribute("u",bean);
				return "aDKUserInfo/adduser";
			}*/
}
